﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Xml.Linq;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.BLL
{
    public class BLLT_B_Group
    {
        public T_Base<T_B_Group> GetList(string xml)
        {
            T_Base<T_B_Group> list = new T_Base<T_B_Group>();
            SqlParameter[] para = { 
                                  new SqlParameter("@para",SqlDbType.Xml,-1)
                                  };
            para[0].Value = xml;
            #region 获取数据
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetGroup", para))
            {
                List<T_B_Group> inner = new List<T_B_Group>();
                if (dr.Read() && dr.HasRows)
                    list.total = dr.GetInt32(0);
                if (dr.NextResult())
                {
                    while (dr.Read() && dr.HasRows)
                    {
                        T_B_Group dto = new T_B_Group();
                        dto.GID = dr.GetGuid(0);
                        dto.IsFunction = dr.GetBoolean(1);
                        dto.GName = dr.GetString(2);
                        dto.GNote = dr.GetString(3);
                        dto.IsValid = dr.GetBoolean(4);
                        inner.Add(dto);
                    }
                    list.data = inner;
                }
            }
            #endregion
            return list;
        }

        /// <summary>
        /// 操作数据
        /// </summary>
        /// <param name="xml">第一个为添加 ；第二个为修改；第三个为删除；</param>
        /// <returns></returns>
        public string Save(params IEnumerable<T_B_Group>[] xml)
        {
            string addXmL = EFHelp<T_B_Group>.GetXML(xml[0]);
            string editXML = EFHelp<T_B_Group>.GetXML(xml[1]);
            string delXML = EFHelp<T_B_Group>.GetXML(xml[2]);
            SqlParameter[] para = { 
                                  new SqlParameter("@addXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@editXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@delXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@userXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXmL;
            para[1].Value = editXML;
            para[2].Value = delXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveGroup", para);
            return (para[4].Value ?? string.Empty).ToString();
        }

        public string SaveGroupMenu(Guid gid, IEnumerable<T_B_GroupMenu> xml)
        {
            string editXML = EFHelp.GetXML(xml);
            SqlParameter[] para = { 
                                      new SqlParameter("@GID",SqlDbType.UniqueIdentifier,16),
                                      new SqlParameter("@editXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                   };
            para[0].Value = gid;
            para[1].Value = editXML;
            para[2].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, "P_B_SaveGroupMenu", para);

            return (para[2].Value ?? string.Empty).ToString();
        }

        public List<T_B_GroupMenu> GetGroupMenu(Guid gid)
        {
            List<T_B_GroupMenu> list = new List<T_B_GroupMenu>();
            SqlParameter[] para = { 
                                      new SqlParameter("@gid",SqlDbType.UniqueIdentifier,16)
                                   };
            para[0].Value = gid;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetGroupMenu", para))
            {
                while (dr.Read() && dr.HasRows)
                {
                    T_B_GroupMenu dto = new T_B_GroupMenu();
                    dto.MID = dr.GetGuid(0);
                    dto.FID = dr.GetValue(1) is DBNull ? Guid.Empty : dr.GetGuid(1);
                    dto.MName = dr.GetString(2);
                    dto.IsMID = dr.GetValue(3) is DBNull ? Guid.Empty : dr.GetGuid(3);
                    if (!(dr.GetValue(4) is DBNull))
                    {
                        dto.ButtonXML = dr.GetSqlXml(4).Value;
                        dto.data = new List<T_B_MenuXML>();
                        XElement.Parse(dto.ButtonXML).Elements().ToList().ForEach(a =>
                        {
                            T_B_MenuXML _inner = new T_B_MenuXML()
                            {
                                ID = a.Attribute("ID").Value,
                                Text = a.Attribute("Text").Value
                            };

                            if (!(dr.GetValue(5) is DBNull))//是否有子节点
                            {
                                dto.InnerXML = dr.GetSqlXml(5).Value;
                                if (XElement.Parse(dto.InnerXML).Elements().Attributes("ID").ToList().Exists(b => b.Value == _inner.ID))
                                {
                                    _inner.IsChecked = true;
                                }
                            }
                            dto.data.Add(_inner);
                        });
                    }
                    list.Add(dto);
                }
            }

            return list;
        }

        #region  组与用户

        public DataTable GetUnSelectedUser(string gid)
        {
            DataTable dt = new DataTable();

            try
            {
                SqlParameter[] para ={
                                        new SqlParameter("@GID",SqlDbType.NVarChar,200)
                                    };

                para[0].Value = gid;

                dt = SqlHelper.ExecuteProcedureToDataSet("P_B_Group_GetUnSelectedUser", para, "dt").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            return dt;
        }

        public T_Base<T_B_User> GetSelectedUser(string gid, string xml)
        {
            T_Base<T_B_User> list = new T_Base<T_B_User>();

            SqlParameter[] para = { 
                                      new SqlParameter("@GID",SqlDbType.NVarChar,200),
                                      new SqlParameter("@Key",SqlDbType.Xml,-1)
                                  };

            para[0].Value = gid;
            para[1].Value = xml;

            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_Group_GetSelectedUser", para))
            {
                List<T_B_User> inner = new List<T_B_User>();

                if (dr.Read() && dr.HasRows)
                    list.total = dr.GetInt32(0);
                if (dr.NextResult())
                {
                    while (dr.Read() && dr.HasRows)
                    {
                        T_B_User dto = new T_B_User();
                        dto.UID = dr.GetGuid(0);
                        dto.UName = dr.GetString(1);
                        dto.CName = dr.GetString(2);
                        inner.Add(dto);
                    }
                }
                list.data = inner;
            }

            return list;
        }

        public string SaveGroupUser(IEnumerable<T_B_User_Group> add, IEnumerable<T_B_User_Group> remove)
        {
            string addXML = EFHelp<T_B_User_Group>.GetXML(add);
            string removeXML = EFHelp<T_B_User_Group>.GetXML(remove);

            SqlParameter[] para = { 
                                      new SqlParameter("@AddXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@RemoveXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@UserXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                   };

            para[0].Value = addXML;
            para[1].Value = removeXML;
            para[2].Value = null;
            para[3].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, "P_B_Group_SaveUserGroup", para);

            return (para[3].Value ?? string.Empty).ToString();
        }

        #endregion

        #region 组与公司

        public DataTable GetAllCompany(string gid)
        {
            DataTable dt = new DataTable();

            try
            {
                SqlParameter[] para ={
                                        new SqlParameter("@GID",SqlDbType.NVarChar,200)
                                    };

                para[0].Value = gid;

                dt = SqlHelper.ExecuteProcedureToDataSet("P_B_Group_GetAllCompany", para, "dt").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            return dt;
        }

        public string SaveGroupCompanyData(IEnumerable<T_B_Company_Group> edit, string gid)
        {
            string editXML = EFHelp<T_B_Company_Group>.GetXML(edit);

            SqlParameter[] para = { 
                                      new SqlParameter("@EditXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@GID",SqlDbType.NVarChar,100),
                                      new SqlParameter("@UserXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                   };

            para[0].Value = editXML;
            para[1].Value = gid;
            para[2].Value = null;
            para[3].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, "P_B_Group_SaveGroupCompanyData", para);

            return (para[3].Value ?? string.Empty).ToString();
        }

        #endregion
    }
}
